/*
* OutputHdl.c
*
* Created: 15.02.2013 21:12:19
*  Author: victor
*/

#include <Basetypes.h>
#include <OutputHdl.h>
#include <avr/io.h>
#include <DIODrv.h>
#include <CommonDefinitions.h>
#include <SerialHdl.h>
#include <TimerDrv.h>

/* Definition of local macros */
/* 55 miliseconds at 1ms decreasing step */
#define OUTPUTHDL_C_AHUNDRED_MS_U8       (u8)110
#define OUTPUTHDL_C_FIVE_THOUSAND_MS_U16 (u16)5500 

/* Definition of local variables */
static u8 tmp_command_u8;
static u16 OutputHdl_ms_cnt_u16;

/* Project global variables */
u8 OutputHdl_ClearToSendFLAG_u8;

/* Flag that indicates that the pin has to be cleared */
static u8 tmp_clear_u8;
void OutputHdl_Init ( void )
{
	OutputHdl_ms_cnt_u16 = ZERO;
	tmp_command_u8 = ZERO;
	tmp_clear_u8 = ZERO;
}

void OutputHdl_Command ( EN_CommonDefinition_CommandsEnum command )
{
	/* Check if the previous command was entirely processed */
	//while ( ZERO != tmp_command_u8 )
	//{
	//}
	if ( ZERO != tmp_command_u8 )
	{
		return;
	}
	
	else
	{
		/* Acknowledge the module that there is a command to be served */
		tmp_command_u8 = ONE;
		
		/* Evaluate the command */
		switch ( command )
		{
			case PHONE_ANS_HUP_EN:
				{
					/*Send Pin SET command to the DIODrv*/
					DIODrv_SetPin ( PHONE_ANS_HUP_EN );
					/* Load the counter that will be decremented each millisecond, for 50 ms or more*/
					OutputHdl_ms_cnt_u16 = OUTPUTHDL_C_AHUNDRED_MS_U8;
					/* Set flag that indicates that the pin has to be cleared */
					tmp_clear_u8 = ONE;
					break;
				}
				
			case SUPPLY_RST_EN:
				{
					/*Send Pin SET command to the DIODrv*/
					DIODrv_SetPin ( SUPPLY_RST_EN );
					/* Load the counter that will be decremented each millisecond, for 1 seconds or more*/
					OutputHdl_ms_cnt_u16 = OUTPUTHDL_C_AHUNDRED_MS_U8;
					/* Set flag that indicates that the pin has to be cleared */
					tmp_clear_u8 = ONE;
					break;
				}
				
			case POWER_ON_EN:
				{
					/*Send Pin SET command to the DIODrv*/
					DIODrv_SetPin ( POWER_ON_EN );
					/* Load the timer1 value and reset the counter, the pin will automaticaly cleared at output compare match */
					TimerDrv_ResetTimer1 ( 5 );
					OutputHdl_ms_cnt_u16 = OUTPUTHDL_C_FIVE_THOUSAND_MS_U16;
					break;
				}
				
			case PLAY_TRACK_EN:
				{
					/*Send Pin SET command to the DIODrv*/
					DIODrv_SetPin ( PLAY_TRACK_EN );
					/* Load the counter that will be decremented each millisecond, for 50 ms or more*/
					OutputHdl_ms_cnt_u16 = OUTPUTHDL_C_AHUNDRED_MS_U8;
					/* Set flag that indicates that the pin has to be cleared */
					tmp_clear_u8 = ONE;
					break;
				}
				
			case PAUSE_TRACK_EN:
				{
					/*Send Pin SET command to the DIODrv*/
					DIODrv_SetPin ( PAUSE_TRACK_EN );
					/* Load the counter that will be decremented each millisecond, for 50 ms or more*/
					OutputHdl_ms_cnt_u16 = OUTPUTHDL_C_AHUNDRED_MS_U8;
					/* Set flag that indicates that the pin has to be cleared */
					tmp_clear_u8 = ONE;
					break;
				}
				
			case NEXT_TRACK_EN:
				{
					/*Send Pin SET command to the DIODrv*/
					DIODrv_SetPin ( NEXT_TRACK_EN );
					/* Load the counter that will be decremented each millisecond, for 50 ms or more*/
					OutputHdl_ms_cnt_u16 = OUTPUTHDL_C_AHUNDRED_MS_U8;
					/* Set flag that indicates that the pin has to be cleared */
					tmp_clear_u8 = ONE;
					break;
				}
				
			case PREVIOUS_TRACK_EN:
				{
					/*Send Pin SET command to the DIODrv*/
					DIODrv_SetPin ( PREVIOUS_TRACK_EN );
					/* Load the counter that will be decremented each millisecond, for 50 ms or more*/
					OutputHdl_ms_cnt_u16 = OUTPUTHDL_C_AHUNDRED_MS_U8;
					/* Set flag that indicates that the pin has to be cleared */
					tmp_clear_u8 = ONE;
					break;
				}
				
			case FAST_FWD_EN:
				{
					/*Send Pin SET command to the DIODrv*/
					DIODrv_TogglePin ( FAST_FWD_EN );
					/* Clear flag that indicates that the pin has to be cleared */
					tmp_clear_u8 = ZERO;
					break;
				}
				
			case REVERSE_EN:
				{
					/*Send Pin SET command to the DIODrv*/
					DIODrv_TogglePin ( REVERSE_EN );
					/* Clear flag that indicates that the pin has to be cleared */
					tmp_clear_u8 = ZERO;
					break;
				}
				
			default:
				{
					tmp_command_u8 = ZERO;
					break;
				}
		}
	}
}

void OutputHdl_PBCheck ( void )
{
	/* If there is a command to be processed */
	if ( ONE == tmp_command_u8 )
	{
		/* Decrement the counter each millisecond until it reaches 0 */
		if ( ZERO != OutputHdl_ms_cnt_u16 )
		{
			OutputHdl_ms_cnt_u16--;
		}
		
		/* When the counter reaches 0 and a command that needs pin clear was made */
		else if ( tmp_clear_u8 == ONE )
		{
			/* Clear all bits that were set before */
			DIODrv_ClearPin ( ALL_EN );
			/* Acknowledge the function that the command was served */
			tmp_command_u8 = ZERO;
		}
		
		/* When no timer has to elapse and no command that needs pin to be cleared, just clear the command flag */
		else
		{
			/* do nothing */
			tmp_command_u8 = ZERO;
		}
	}
	
	else
	{
		/* do nothing */
	}
	
}

void OutputHdl_SerSendBackConfirmation ( const u16* confirmation, u8 len )
{
	u8 tmp_NoOfElementsCnt_u8;
	
	for ( tmp_NoOfElementsCnt_u8 = ZERO; tmp_NoOfElementsCnt_u8 <= ( u8 ) ( len - 1 ); tmp_NoOfElementsCnt_u8++ )
	{
		while ( OutputHdl_ClearToSendFLAG_u8 != ONE )
		{
		
		}
		
		OutputHdl_ClearToSendFLAG_u8 = ZERO;
		SerialHdl_SendData ( confirmation[tmp_NoOfElementsCnt_u8] );
	}
}